package Classes.GameViews.ViewDisplayers.MapTargets// ActionScript file
{
	import Classes.GameDatas.GD_Building;
	import Classes.GameDatas.GD_Building_Sundry;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameUtils.GU_GetBMPMC;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.GV_ZombieHotPanel;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Interfaces.I_Bar;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	/**
	 *建筑显示类 
	 * @author 葛增文
	 * 
	 */	
	public class MT_Building extends MapTarget implements I_NeedSWFSource,I_Bar
	{
		private var _img:DisplayObject;/*建筑外观*/
		private var _hotPanel:GV_ZombieHotPanel;
		public var mtBuild_effect_container:Sprite=new Sprite();
		private var _tmpSign:Boolean;
		private var _nowPicStr:String;
		public function MT_Building(gd:GD_Building):void
		{
			super(gd);
			this.init();
		}
		
		private function init():void
		{
			this.mouseEnabled=false;
			this.beginDrawMyView();
		}
		
		private function sourceLoad():void
		{
			M_LoadSWFSource.instance.needLoad(this,M_ConfigData.instance.imgUrl+this.buildingData.buildingPicSourceStr,this.buildingData.buildingPicSourceStr);
		}
		
		public function sourceLoadOver(sourceName:String):void
		{
			this.draw();
		}
		
		override protected function beginDrawMyView():void
		{
			if(!this.nowPicStr || this.nowPicStr!=this.buildingData.buildingPicStr)
			{
				this.sourceLoad();
				this.nowPicStr=this.buildingData.buildingPicStr;
			}
		}
		
		private function draw():void
		{
			if(!this.buildingData)return;
			if(this.img)this.removeChild(this.img);
			this._img=GU_GetBMPMC.getBMPMC(this.buildingData.buildingPicStr,this.tmpSign);
			this.addChild(this.img);
			var gridSize:Number=GV_Map.instance.gridSize;
			this.img.y+=this.buildingData.size*gridSize/2;
			this.img.x-=this.buildingData.size*gridSize;
			this.addListener();
			
			if(!this._hotPanel){
				this._hotPanel=new GV_ZombieHotPanel(this.buildingData);
				this._hotPanel.visible=false;
				this._hotPanel.scaleX=0.5;
				this._hotPanel.scaleY=0.5;
				this._hotPanel.visible=false;
			}
			this.addChild(this._hotPanel);
			
			if(this.buildingData.mirrorPar==1)
			{
				this.img.scaleX=-1;
				this.img.x+=this.img.width;
			}else{
				this._hotPanel.y=-this._img.y-10;
			}
			
			this.addChild(this.mtBuild_effect_container);
			this.mtBuild_effect_container.y=this._img.y-this._img.height+20;
			
			this.updataDeepIndex();
		}
		
			
		private var callFun:Function;
		private var timer:Timer;
		/**
		 * 为杂物增加额外效果 
		 * @param dis 
		 * @param callBack
		 */	
		public function addEffect(dis:DisplayObject,callBack:Function):void
		{
			this.callFun=callBack;
			while(this.mtBuild_effect_container.numChildren>0)this.mtBuild_effect_container.removeChildAt(0);
			this.mtBuild_effect_container.addChild(dis);
			timer=new Timer(500,5);
//			timer.addEventListener(TimerEvent.TIMER,timerHalder);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
			timer.start();
		}
		
		private function completeTimerHalder(evt:TimerEvent):void
		{
			if(null!=this.callFun)this.callFun();
			while(this.mtBuild_effect_container.numChildren>0)this.mtBuild_effect_container.removeChildAt(0);
		}
		
		private function addListener():void
		{
			//			this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			//			this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}
		
		private function removeListener():void
		{
			//			this.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
			//			this.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}
		
		
		private function overHandler(e:MouseEvent):void
		{
			this._hotPanel.visible=true;
		}
		
		private function outHandler(e:MouseEvent):void
		{
			this._hotPanel.visible=false;
		}
		
		
		/**
		 *创建一个建筑 
		 * @param creatData 数据对象
		 * @return 建筑对象
		 * 
		 */			
		public static function creat(creatData:GD_Building):MT_Building
		{
			var returnBuilding:MT_Building;
			returnBuilding=new MT_Building(creatData);
			return returnBuilding;
		}
		
		/**
		 *找到相关联的地图格数组 
		 * @param mapGrid 对象地图格
		 * @return 关联地图格数组
		 * 
		 */		
		public function getRelevanceGridsAry(mapGrid:GD_MapGrid):Array
		{
			var returnAry:Array=new Array;
			for(var i:int=0;i<this.size;i++)
			{
				for(var j:int=0;j<this.size;j++)
				{
					var relevanceGrid:GD_MapGrid=GV_Map.instance.getCoordinateGrid(new Point(mapGrid.atx+i,mapGrid.aty+j));
					returnAry.push(relevanceGrid);
				}
			}
			return returnAry;
		}
		
		/**
		 *获得顶点数组 
		 * @return 顶点数组
		 * 
		 */		
		override public function getVertexsAry():Array
		{
			var gridPoint:Point=new Point(this.mapGrid.atx,this.mapGrid.aty);
			var p1:Point=new Point(gridPoint.x-1,gridPoint.y-1);
			var p2:Point=new Point(gridPoint.x+this.size,gridPoint.y-1);
			var p3:Point=new Point(gridPoint.x+this.size,gridPoint.y+this.size);
			var p4:Point=new Point(gridPoint.x-1,gridPoint.y+this.size);
			var returnAry:Array=[p1,p2,p3,p4];
			for(var i:int=0;i<returnAry.length;i++)
			{
				var checkP:Point=returnAry[i];
				if(!GV_Map.instance.checkPoint(checkP))
				{
					returnAry.splice(i,1);
					i--;
				}
			}
			return returnAry;
		}
		
		/**
		 *覆写碰撞检测 
		 * @param checkX
		 * @param checkY
		 * @return 
		 * 
		 */		
		override public function hitTestPointCheck(checkX:Number, checkY:Number):Boolean
		{
			if(!this.img)return false;
			if(super.hitTestPointCheck(checkX,checkY))
			{
				var checkTarget:MovieClip_BMP=(this.img as Sprite).getChildAt(0) as MovieClip_BMP;
				if(this.buildingData.mirrorPar)
				{
					checkX+=this.img.width;
					/*var tmpPoint:Point=this.globalToLocal(new Point(checkX,checkY));
					tmpPoint.x=-tmpPoint.x+this.img.width;
					checkX=this.localToGlobal(tmpPoint).x;*/
				}
				return checkTarget.hitTest(new Point(checkX,checkY));
			}
			return false;
		}
		
		/**
		 *建立或消除建筑与地图格的关联 
		 * @param mapGrid 
		 * @param sign true:建立关联 false:消除关联
		 * 
		 */				
		public function buildRelevanceWithGrid(mapGrid:GD_MapGrid,sign:Boolean):void
		{
			if(!mapGrid)return;
			var relevanceGridsAry:Array=this.getRelevanceGridsAry(mapGrid);
			for each(var checkGrid:GD_MapGrid in relevanceGridsAry)
			{
				if(sign)
				{
					checkGrid.building=this;
				}
				else if(checkGrid.building==this)
				{
					checkGrid.building=null;
				}
			}
		}
		
		public function removeFromMap():void
		{
			if(this.parent)this.parent.removeChild(this);
			this.mapGrid=null;
		}
		
		override public function release():void
		{
			if( GV_Map.instance && GV_Map.instance.moveHot) 
			GV_Map.instance.moveHot.visible	=	false;
			
			this.removeListener();
			super.release();
			if(null!=this._hotPanel)
			this._hotPanel.release();
			this._hotPanel=null;
			this.mapGrid=null;
			this._img=null;
		}
		
		/*getter & setter*/
		override public function set mapGrid(value:GD_MapGrid):void
		{
			var relevanceGridsAry:Array;
			var checkGrid:GD_MapGrid;
			if(this.mapGrid)this.buildRelevanceWithGrid(this.mapGrid,false);
			super.mapGrid=value;
			if(this.mapGrid)this.buildRelevanceWithGrid(this.mapGrid,true);
			if(this.buildingData)this.buildingData.updateCoordinate();
		}
		
		public function get size():int
		{
			return this.buildingData.size;
		}
		
		override public function get deepIndex():Number
		{
			return _deepIndex;
		}
		
		override public function updataDeepIndex():void
		{
			this._deepIndex		=	this.y+GV_Map.instance.gridSize*(this.size-1)/2;
		}
		
		public function get buildingData():GD_Building
		{
			return this.gameData as GD_Building;
		}
		
		public function get img():DisplayObject
		{
			return _img;
		}
		
		override public function get selectPriority():int
		{
			return 10;
		}
		
		/**
		 * 覆定发光，加上显示热感。去掉了，mouser_out,mouser_ROLL_OVER事件 
		 * @param value
		 * 
		 */
		override public function setGlowFilter(value:Boolean):void
		{
			//if(this._hotPanel)0.
			try{
				if(this._hotPanel){
//					this._hotPanel.visible	= 	value;
					var ss:GV_ZombieHotPanel=GV_Map.instance.moveHot;
					if(GV_Map.instance.moveHot)GV_Map.instance.moveHot.release();
					GV_Map.instance.moveHot	=	new GV_ZombieHotPanel(this.buildingData);
					
					GV_Map.instance.addChild(GV_Map.instance.moveHot);
					GV_Map.instance.moveHot.scaleX	=	this._hotPanel.scaleX;
					GV_Map.instance.moveHot.scaleY	=	this._hotPanel.scaleY;
					GV_Map.instance.moveHot.alpha	=	this._hotPanel.alpha;
					GV_Map.instance.moveHot.transform	=	this._hotPanel.transform;
					
					
					
					GV_Map.instance.moveHot.x 	=	this._hotPanel.x+this._hotPanel.parent.x;
					GV_Map.instance.moveHot.y 	=	this._hotPanel.y+this._hotPanel.parent.y;
					
					
//					this._hotPanel.visible			=	false;
					GV_Map.instance.moveHot.visible	=	value;
				}
				super.setGlowFilter(value);
				
			}catch(e:Error){
				VP_errorPanel.instance.setErrorMessage("热感出错");
				VP_errorPanel.instance.enterFrame();
			}
		}

		public function get tmpSign():Boolean
		{
			return _tmpSign;
		}

		public function set tmpSign(value:Boolean):void
		{
			_tmpSign = value;
		}

		public function get nowPicStr():String
		{
			return _nowPicStr;
		}

		public function set nowPicStr(value:String):void
		{
			_nowPicStr = value;
		}

		
	}
}